
#ifndef __APR_AUTO_MEMORY_POOL_H
#define __APR_AUTO_MEMORY_POOL_H

#include <apr_pools.h>

/** An automated way to create and destroy an APR memory pool
 * following the scoping of the class. The 'real' pool is extracted
 * from the object by mp(). */
class APRAutoMemoryPool
{
   public:
      /** Constructor for the automated memory pool.
       * 
       * @param parent_memory_pool The memory pool to derive this memory pool
       * from. */
      APRAutoMemoryPool(apr_pool_t *parent_memory_pool)
      {
         apr_pool_create(&real_mp, parent_memory_pool);
         parent_mp = parent_memory_pool;
      }
      /** Destructor for the automated memory pool. */
      ~APRAutoMemoryPool()
      {
         apr_pool_destroy(real_mp);
      }
      /** Gets the memory pool, valid only in the scope of the class.
       *
       * @return The memory pool. */
      apr_pool_t *mp()
      {
         return real_mp;
      }
      /** Destroys and reinstantiates the memory pool, freeing everything
       * that was previously in it and creating a new pool to allocate further
       * objects from. */
      void wipe()
      {
         apr_pool_destroy(real_mp);
         apr_pool_create(&real_mp, parent_mp);
      }
   private:
      /** The 'real' memory pool. */
      apr_pool_t *real_mp;
      /** The parent memory pool, kept in order to rederive new pools. */
      apr_pool_t *parent_mp;
};

#endif
